package org.dromara.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.GoodsOrder;
import org.dromara.business.domain.bo.GoodsOrderBo;
import org.dromara.business.domain.vo.GoodsOrderVo;
import org.dromara.business.service.GoodsOrderService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 订单
 *
 * @author Lion Li
 * @date 2025-06-13
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/order")
public class GoodsOrderController extends BaseController {

    private final GoodsOrderService goodsOrderService;

    /**
     * 查询订单列表
     */
//    @SaCheckPermission("business:order:list")
    @GetMapping("/list")
    public TableDataInfo<GoodsOrderVo> list(GoodsOrderBo bo, PageQuery pageQuery) {
        return goodsOrderService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单列表
     */
    @SaCheckPermission("business:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GoodsOrderBo bo, HttpServletResponse response) {
        List<GoodsOrderVo> list = goodsOrderService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单", GoodsOrderVo.class, response);
    }

    /**
     * 获取订单详细信息
     *
     * @param goodsOrderId 主键
     */
    @SaCheckPermission("business:order:query")
    @GetMapping("/{goodsOrderId}")
    public R<GoodsOrderVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long goodsOrderId) {
        return R.ok(goodsOrderService.queryById(goodsOrderId));
    }

    /**
     * 新增订单
     */
    @SaCheckPermission("business:order:add")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GoodsOrderBo bo) {
        return toAjax(goodsOrderService.insertByBo(bo));
    }

    /**
     * 修改订单
     */
    @SaCheckPermission("business:order:edit")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GoodsOrderBo bo) {
        return toAjax(goodsOrderService.updateByBo(bo));
    }

    /**
     * 删除订单
     *
     * @param goodsOrderIds 主键串
     */
//    @SaCheckPermission("business:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsOrderIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] goodsOrderIds) {
        return toAjax(goodsOrderService.deleteWithValidByIds(List.of(goodsOrderIds), true));
    }

    /**
     * 直接下单
     */
    @Log(title = "直接下单", businessType = BusinessType.INSERT)
    @RepeatSubmit(interval = 2000, message = "订单正在提交...")
    @PostMapping("/addOrder")
    public R<GoodsOrder> addOrder(@RequestBody GoodsOrderBo bo) {
        return R.ok(goodsOrderService.addOrder(bo, 0));
    }

    /**
     * 微信支付预下单
     */
    @Log(title = "微信支付预下单", businessType = BusinessType.INSERT)
    @RepeatSubmit(interval = 2000, message = "订单正在提交...")
    @PostMapping("/addOrderTopUp")
    public R<GoodsOrder> addOrderTopUp(@RequestBody GoodsOrderBo bo) {
        return R.ok(goodsOrderService.addOrderTopUp(bo, null));
    }

    /**
     * 购物车下单
     *
     * @param shoppingCartId 购物车id
     * @param bo             订单信息
     * @return
     */
    @RepeatSubmit(interval = 2000, message = "订单正在提交...")
    @PostMapping("/createOrderByShoppingCartId")
    public R<GoodsOrder> createOrderByShoppingCartId(Long[] shoppingCartId, GoodsOrderBo bo) {
        return R.ok(goodsOrderService.createOrderByShoppingCartId(shoppingCartId, bo));
    }

    /**
     * 购物车下单微信支付
     *
     * @param shoppingCartId 购物车id
     * @param bo             订单信息
     * @return
     */
    @RepeatSubmit(interval = 2000, message = "订单正在提交...")
    @PostMapping("/createPayOrderByShoppingCartId")
    public R<GoodsOrder> createPayOrderByShoppingCartId(Long[] shoppingCartId, GoodsOrderBo bo) {
        return R.ok(goodsOrderService.createPayOrderByShoppingCartId(shoppingCartId, bo));
    }

    /**
     * 抢单大厅列表
     */
    /**
     * 抢单大厅列表
     */
    @GetMapping("/unCheckList")
    public TableDataInfo<GoodsOrderVo> unCheckList(PageQuery pageQuery) {
        return TableDataInfo.build(goodsOrderService.unCheckList(pageQuery));
    }

    /**
     * 抢单
     */
    @RepeatSubmit(interval = 1500, message = "正在抢单...")
    @PutMapping("/check")
    public R<Void> check(@RequestParam Long goodsOrderId) {
        return toAjax(goodsOrderService.check(goodsOrderId));
    }

    /**
     * 派单
     */
    @PutMapping("/dispatch")
    @RepeatSubmit(interval = 1500, message = "请勿重复操作")
    public R<Void> dispatch(Long orderId, Long userId) {
        return toAjax(goodsOrderService.dispatch(orderId, userId));
    }

    /**
     * 取消订单
     */
    @PutMapping("/remove")
    @RepeatSubmit(interval = 1500, message = "正在取消...")
    public R<Void> removeOrder(Long orderId) {
        return toAjax(goodsOrderService.removeOrder(orderId));
    }

    /**
     * 打手完成订单
     *
     * @param orderId 订单id
     */
    @PutMapping("/waiterEnd")
    @RepeatSubmit(interval = 1500, message = "正在提交...")
    public R<Void> waiterEnd(@RequestParam Long orderId, @NotBlank(message = "请上传服务完成截图") @RequestParam String serverImg, String serverRemark) {
        return toAjax(goodsOrderService.waiterEnd(orderId, serverImg, serverRemark));
    }

    /**
     * 客户完成订单
     *
     * @param orderId 订单id
     */
    @PutMapping("/end")
    @RepeatSubmit(interval = 1500, message = "正在提交...")
    public R<Void> end(Long orderId) {
        return toAjax(goodsOrderService.end(orderId));
    }

    /**
     * 将订单放回大厅订单
     *
     * @param orderId 订单id
     */
    @SaCheckPermission("business:order:edit")
    @PutMapping("/again")
    @RepeatSubmit(interval = 1500, message = "正在提交...")
    public R<Void> again(Long orderId) {
        return toAjax(goodsOrderService.again(orderId));
    }

    @PutMapping("/addMember")
    @RepeatSubmit(interval = 1500, message = "正在提交...")
    public R<Void> addMember(@RequestParam Long orderId, Long[] userIds) {
        return toAjax(goodsOrderService.addMember(orderId, Arrays.asList(userIds)));
    }

    /**
     * 退款
     */
    @SaCheckPermission("business:order:edit")
    @RepeatSubmit(interval = 1500, message = "正在提交...")
    @PutMapping("/refund")
    public R<Void> refund(@RequestParam Long orderId) {
        return toAjax(goodsOrderService.refund(orderId));
    }

    /**
     * 首页订单播报
     */
    @SaIgnore
    @GetMapping("/getOrderNotice")
    public R<List<String>> getOrderNotice() {
        return R.ok(goodsOrderService.getOrderNotice());
    }
}
